package com.treasure.service.collocate.controller;

import com.treasure.service.annotation.LogInfo;
import com.treasure.service.collocate.service.ArticleService;
import com.treasure.service.commons.lang.util.Base64Util;
import com.treasure.service.commons.lang.util.BeanUtil;
import com.treasure.service.commons.lang.util.IdCreater;
import com.treasure.service.domain.TArticle;
import com.treasure.service.dto.TArticleDto;
import com.treasure.service.result.PageResult;
import com.treasure.service.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 资讯，文章信息维护
 * @version 1.0
 * @author panxinwen 2018/10/30 10:32
 * @Copyright © 2015 | 武汉京航科技有限公司 Email:19919866605@163.com
 * */
@Api(value = "资讯，文章信息维护",description = "资讯，文章信息维护")
@RestController
@RequestMapping("/article/v1")
public class ArticleController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ArticleService articleService;

    @Value("${treasure.file.path}")
    private String IMG_PATH;

    @ApiOperation(value = "获取资讯列表", notes = "获取资讯列表")
    @PostMapping("/getArticleList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "頁碼", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每頁顯示數量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "当前状态(0-禁用 1-启用)", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "recommend", value = "是否首页展示(0-否 1-是)", required = true, dataType = "int", paramType = "query"),
    })
    @LogInfo(log = "获取资讯列表",isPrint = true)
    public ResponseEntity getArticleList(@RequestParam(value = "pageSize") Integer pageSize,
                                         @RequestParam(value = "pageNum") Integer pageNum,
                                         @RequestParam(value = "status",required = false) Integer status,
                                         @RequestParam(value = "recommend",required = false) Integer recommend
    ){

        PageResult<List<TArticleDto>> result = articleService.getArticleList(pageSize,pageNum,status,recommend);

        return ResponseEntity.ok(result);

    }

    @ApiOperation(value = "获取咨询详情", notes = "获取咨询详情")
    @PostMapping("/getArticleInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "获取咨询详情", required = true, dataType = "long", paramType = "query"),
    })
    @LogInfo(log = "获取咨询详情",isPrint = true)
    public ResponseEntity getArticleInfo(@RequestBody TArticle tArticle){

        TArticleDto tArticleDto = articleService.getArticleInfo(tArticle);

        return ResponseEntity.ok(tArticleDto);

    }

    @ApiOperation(value = "新建或更新资讯信息", notes = "新建或更新资讯信息")
    @PostMapping("/updateArticle")
    @LogInfo(log = "新建或更新资讯信息",isPrint = true)
    public ResponseEntity updateArticle(@RequestBody TArticleDto tArticle){

        String img =tArticle.getImg().replace("data:image/jpeg;base64,", "");
        String imgName = IdCreater.getLongId()+".jpg";
        imgName = Base64Util.generateImage(img, IMG_PATH+imgName);
        tArticle.setHeadPic(imgName);
        articleService.updateArticle(BeanUtil.copy(tArticle,TArticle::new));

        return ResponseEntity.ok(new ResponseResult());

    }

}
